<template>
	<view class="container">
		<view class="title">连接设备Wi-Fi</view>
		<image class="explain-img" :src="$IMAGE_BASE_URL + '/static/bg_wifi_config.png'" />
		<view class="form">
			<view class="form-item">
				<image class="form-icon" :src="$IMAGE_BASE_URL + '/static/ic_wifi.png'" />
				<input class="form-input" type="text" v-model="wifiName" placeholder="请输入wifi名称" />
				<view class="more-network" @click="showWifiList">
					<view class="network-text">更多网络</view>
					<u-icon name="arrow-right" size="24" color="#FFFFFF" />
				</view>
			</view>
			<view class="form-item">
				<image class="form-icon" :src="$IMAGE_BASE_URL + '/static/ic_lock.png'" />
				<input class="form-input" placeholder="请输入wifi密码" v-model="password" :password="isPassword" />
				<view class="down-icon" @click="onSetPassword">
					<u-icon v-if="!isPassword" name="eye-fill" size="40" color="#010101" />
					<u-icon v-else name="eye-off" size="40" color="#010101" />
				</view>
			</view>
			<view class="tip">提示：如没有WiFi密码请先设置</view>
		</view>
		<BaseButton class="btn-wrapper" shape="circle" text="下一步" @click="startConfig" />
		
		<UpdateDialog :show="isUpdateDialog" :text="configStep" />
	</view>
</template>

<script>
	import BaseButton from "@/components/base-button/index.vue";
	import UpdateDialog from "./components/update-dialog/index.vue";
	import {
		showInfo
	} from "@/utils/ToastUtils.js";
	import BluWifiConfigService from "@/service/BluWifiConfigService";
	import StorageUtils from "@/utils/StorageUtils";
	import * as DeviceApi from '@/api/device/index.js'

	export default {
		data() {
			return {
				blueMac: "",
				productKey: "",
				blueName: "",
				productInfo: "",
				wifiName: "",
				password: "",
				configStep: "配网",
				jumpFlag: true,
				isPassword: true,
				isUpdateDialog: false,
			};
		},
		components: {
			BaseButton,
			UpdateDialog
		},
		onLoad(options) {
			console.log("this.$addDeviceService.uuid", this.$addDeviceService.uuid);
			console.log("options", options);
			this.bluWifiConfigService = new BluWifiConfigService();
			this.blueMac = options.blueMac;
			this.productKey = options.productKey;
			this.initWifi();
		},
		methods: {
			initWifi() {
				uni.startWifi({
					success: (res) => {
						console.log("startWifi success", res);
						this.getCurrentWifi();
					},
					fail: (err) => {
						console.log("startWifi err", err);
					}
				})
			},
			getCurrentWifi() {
				uni.getConnectedWifi({
					success: (res) => {
						console.log("getConnectedWifi success", res);
						if (!!res && !!res.wifi && !!res.wifi.SSID) {
							this.wifiName = res.wifi.SSID;
						}
					},
					fail: (err) => {
						console.log("getConnectedWifi error", err);
					}
				})
			},
			showWifiList() {
				console.log("查看wifi列表");
				uni.navigateTo({
					url:'/pages/addDevice/configWifiList'
				})
			},
			onWifiChange(e) {
				console.log("onWifiChange", e);
				this.wifiName = e.SSID.replace(/^"|"$/g, '');
			},
			onSetPassword() {
				console.log("显示密码");
				this.isPassword = !this.isPassword;
			},
			// 开始配网
			startConfig() {
				if (this.wifiName == '') {
					console.log("wifi账号不存在");
				}

				if (this.password == '') {
					showInfo("请输入密码");
					return
				}

				console.log("开始配网==============================================");
				this.bluWifiConfigService.config({
					productKey: this.productKey,
					deviceId: this.blueMac,
					wifi: this.wifiName,
					wifiPass: this.password,
				}, {
					start: () => {
						console.log("开始配网");
						this.configStep = "开始配网";
						this.isUpdateDialog = true;
					},
					finish: (deviceInfo) => {
						console.log("配网成功");
						this.configStep = "配网成功";
						console.log("设备配置页面退出");
						this.bluWifiConfigService.closeEvent();
						if(this.$addDeviceService.isBindUser) {
							console.log("需要绑定用户");
							const {
								deviceType,
								deviceName
							} = this.$addDeviceService.device;
							const param = {
								deviceName,
								deviceType,
								uuid: this.$addDeviceService.uuid,
							};
							console.log("用户绑定设备", param);
							DeviceApi.bindDevice(param).then(res => {
								showInfo("绑定成功");
								this.$addDeviceService.isLight = false;
								uni.redirectTo({
									url:'/pages/addDevice/configWifiSuccess'
								})
								// if(this.$addDeviceService.isSubDevice) {
								// 	uni.navigateTo({
								// 		url:'/pages/addDevice/bindLamp'
								// 	})
								// } else {
								// 	this.$addDeviceService.clear();
								// 	uni.switchTab({
								// 		url:'/pages/oxygen/index'
								// 	})
								// }
							}).catch(err => {
								console.log("bindDevice error", err);
								this.toErrorPage();
							})
						} else {
							console.log("不用绑定用户");
							this.$addDeviceService.clear();
							// uni.switchTab({
							// 	url:'/pages/oxygen/index'
							// })
							// uni.navigateBack({
							// 	delta: 3,
							// })
							uni.redirectTo({
								url:'/pages/addDevice/configWifiSuccess'
							})
						}
					},
					step: (index) => {
						let title = "发送wifi";
						switch (index) {
							case 0:
								title = "发送wifi";
								break;
							case 1:
								title = "发送wifi密码";
								break;
							case 2:
								title = "发送url";
								break;
							case 3:
								title = "发送端口";
								break;
							case 4:
								title = "发送clientid";
								break;
							case 5:
								title = "发送用户名";
								break;
							case 6:
								title = "发送密码";
								break;
							case 7:
								title = "发送连接wifi";
								break;
							case 8:
								title = "发送配网";
								break;
						}
						this.configStep = title;
					},
					error: () => {
						console.log("跳转到错误页面");
						this.toErrorPage();
					}
				});
			},
			toErrorPage() {
				if (this.jumpFlag) {
					this.jumpFlag = false;
					console.log("设备配置页面退出");
					this.bluWifiConfigService.closeEvent();
					uni.redirectTo({
						url: '/pages/addDevice/configWifiError?isFlag=0'
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background-color: #FFFFFF;

		.title {
			font-size: 58rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000000;
			padding-top: 100rpx;
			margin-left: 53rpx;
			margin-bottom: 30rpx;
		}

		.explain-img {
			width: 660rpx;
			height: 400rpx;
			display: block;
			margin: 0 auto;
		}

		.form {
			width: 630rpx;
			display: flex;
			flex-direction: column;
			margin: 50rpx auto;

			.form-item {
				width: 100%;
				height: 100rpx;
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;
				font-size: 30rpx;
				margin-right: 20rpx;
				border-bottom: 1px solid #ccc;

				.form-icon {
					width: 50rpx;
					height: 50rpx;
					margin-right: 20rpx;
				}

				.form-input {
					flex: 5;
					padding: 10rpx;
				}

				.down-icon {
					padding: 20rpx 30rpx;
					// background-color: orange;
				}
				
				.more-network {
					width: 130rpx;
					height: 50rpx;
					background-color: #fa7601;
					border-radius: 50rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					
					.network-text {
						font-size: 22rpx;
						font-weight: bold;
						color: #FFF;
					}
				}
			}
			
			.tip {
				font-size: 24rpx;
				margin-top: -10rpx;
				color: #8290a3;
			}
		}

		.btn-wrapper {
			position: fixed;
			bottom: 100rpx;
			left: 50%;
			transform: translateX(-50%);
		}
	}
</style>